package UsersPackage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/alterUser.do")
public class AlterUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

            try {
                String user_id = request.getParameter("userId");
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String email = request.getParameter("email");
                String birth = request.getParameter("birth");
                String phone = request.getParameter("phone");
                String address = request.getParameter("address");
                boolean userPower = request.getParameter("power").isEmpty();

                UserDao userService = new UserDao();

                // Assuming you have a method in UserDao for adding a new user
                if (userService.updateUserByUsername(username, user_id, password, email, birth, phone, address, userPower)>0) {
                    response.getWriter().print("修改用户成功，在3秒后跳转回登录页面!");
                    response.setHeader("Refresh", "3;url=menu/device/2-1.jsp"); // 在3秒后跳转回登录页面
                } else {
                    response.getWriter().print("修改用户失败，在3秒后跳转回登录页面");
                    response.setHeader("Refresh", "3;url=menu/device/2-1.jsp"); // 在3秒后跳转回登录页面
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
